package com.mate.cloud.controller;

import com.mate.cloud.common.web.controller.AdminBaseController;
import com.mate.cloud.domian.OrderMessageDTO;
import com.mate.cloud.rocketmq.producer.OrderProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @description:    java类作用描述
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2025/8/12 16:16
 * @updateUser:     MI
 * @updateTime:     2025/8/12 16:16
 * @updateRemark:   修改内容
 * @version:        1.0
 */

@RestController
@RequestMapping("/order")
public class OrderController extends AdminBaseController {

    @Autowired
    private OrderProducer orderProducer;

    @PostMapping("/create")
    public String createOrder(@RequestParam String orderId) {
        OrderMessageDTO message = new OrderMessageDTO();
        message.setOrderId(orderId);
        message.setAction("CREATE");
        orderProducer.sendTransactionMessage(message); // 事务消息
        return "Order created";
    }

    @PostMapping("/pay")
    public String payOrder(@RequestParam String orderId) {
        OrderMessageDTO message = new OrderMessageDTO();
        message.setOrderId(orderId);
        message.setAction("PAY");
        orderProducer.sendOrderMessage(message); // 顺序消息
        return "Order paid";
    }

    @PostMapping("/cancel")
    public String cancelOrder(@RequestParam String orderId) {
        OrderMessageDTO message = new OrderMessageDTO();
        message.setOrderId(orderId);
        message.setAction("CANCEL");
        message.setDelayLevel(3); // 延迟10秒（级别3对应10s）
        orderProducer.sendDelayMessage(message, 3); // 延迟消息
        return "Order cancellation scheduled";
    }
}
